package com.hebust.controller;

import com.hebust.entity.Admins;
import com.hebust.service.AdminsService;
import com.hebust.service.StudentsService;
import com.hebust.service.TeachersService;
import com.hebust.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController  // 控制层 异步的  所有控制层类都要加
@Api(value = "提供了用户登录等相关接口", tags = "登录模块")  // 类注解  对该控制器类进行功能说明
@CrossOrigin  // 解决跨域问题
public class LoginController {

    @Autowired
    private AdminsService adminsService;

    @Autowired
    private StudentsService studentsService;

    @Autowired
    private TeachersService teachersService;

    @PostMapping("/login")
    @ApiOperation("用户登录接口")  //方法注解  说明方法的作用
    @ApiImplicitParams({  // 加s 表示有多个参数
            @ApiImplicitParam(dataType = "String", name = "username", value = "用户登录账号", required = true),
            @ApiImplicitParam(dataType = "String", name = "password", value = "用户登录密码", required = true),
            @ApiImplicitParam(dataType = "String", name = "type", value = "用户类型", required = true)
    })
    public ResultVo login(String username, String password, String type) {
        if ("1".equals(type)) {  // 管理员登录
            return adminsService.login(username, password);
        } else if ("2".equals(type)) {  // 学生登录
            return teachersService.login(username, password);
        } else {
            return studentsService.login(username, password);
        }
    }

    @GetMapping("/adminList")
    @ApiOperation("管理员查询接口")
    //  @RequestParam(defaultValue = "1")  默认第一页
    public ResultVo show_admins(String username, @RequestParam(defaultValue = "1") int pageNum) {
        return adminsService.show_admins(username, pageNum);
    }


    @PostMapping("/addAdmin")
    @ApiOperation("增加管理员接口")
    public ResultVo add_admins(Admins admins) {
        return adminsService.add_admins(admins);
    }


    @DeleteMapping("/deleteAdmin")
    @ApiOperation("删除管理员接口")
    public ResultVo delete_admins(int id) {
        return adminsService.delete_admins(id);
    }


    @PutMapping("/updateAdmin")
    @ApiOperation("修改管理员接口")
    public ResultVo update_admins(Admins admins) {
        return adminsService.update_admins(admins);
    }

}
